package com.secsys.action.finance;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.secsys.action.BaseAction;
import com.secsys.dto.AssistDto;
import com.secsys.dto.StoFinanceperstockDTO;
import com.secsys.dto.StoStockDTO;
import com.secsys.service.StoFinancePerStockService;
import com.secsys.service.StoStockService;
import com.secsys.util.DateFormatUtils;

/**
 * 处理每股指标中的数据，每股净资产、每股收益、每股收益扣除、每股净资产、每股资本公积金、每股未分配利润等
 * <pre>
 * 1.处理功能（财务分析里面的）:
 * (1).每股净资产的：prepareNetAsset、searchNetAsset、netAssetDetail、doNetAssetSearch分别为默认处理、用户搜索、详细信息、内部协助函数。
 * (2).净资产收益率（每股收益/每股净资产）：prepareNetIncome、searchNetIncome、netAssetIncome、doNetAssetIncome分别为默认处理、用户搜索、详细信息、内部协助函数。
 *@作者 tiger.hu.tiger@gmail.com
 *@创建日期 2011-5-7
 *@版本1.0
 */
public class FinancePerStockAction extends BaseAction<StoFinanceperstockDTO> {
	private StoFinanceperstockDTO stoFinanceperstockDTO;
	private List<StoStockDTO> stockDTOList;
	private AssistDto assistDto;
	private StoStockDTO stockDTO;
	private StoStockService stoStockService;
	
	private StoFinancePerStockService stoFinancePerStockService;
	private List<StoFinanceperstockDTO> stoFinanceperstockDTOList;
	private List<StoFinanceperstockDTO> oldYearStoFinanceperstockDTOList;
	
	/**
	 * 规定分页大小为10
	 */
	private final int limit=10; 
	
	
	
	/**
	 * 处理”净资产收益率（每股收益/每股净资产）“，默认情况处理
	 * @return
	 * @throws Exception
	 */
	public String prepareNetIncome() throws Exception{
		assistDto.setStartDate(DateFormatUtils.convertStringToDate("2008-12-31"));
		assistDto.setEndDate(DateFormatUtils.convertStringToDate("2009-12-31"));
		assistDto.setRate(20);
		assistDto.setUpdown(1);

		this.doNetAssetSearch();
		
		return "search";
	}
	
	/**
	 * 处理”净资产收益率（每股收益/每股净资产）“，用户搜索
	 * @return
	 * @throws Exception
	 */
	public String searchNetIncome() throws Exception{
		if (!this.doNetAssetSearch()) {
			return "prepare";
		}
		return "search";
	}
	
	/**
	 * 处理”净资产收益率（每股收益/每股净资产）“，详细信息显示
	 * @return
	 * @throws Exception
	 */
	public String netIncomeDetail() throws Exception{
		this.stoFinanceperstockDTOList=stoFinancePerStockService.queryStoFinancePerStockByStockCode(stockDTO.getStockCode());
		stockDTO=stoStockService.findByStockCode(stockDTO.getStockCode());
		
		return "detail";
	}
	
	private boolean doNetIncomeSearch(){
		Date startDate=assistDto.getStartDate();
		Date endDate=assistDto.getEndDate();
		double rate=assistDto.getRate();
		int updown=assistDto.getUpdown();
		System.out.println("开始时间="+DateFormatUtils.convertDateTimeToString(startDate)+";结束时间="
				+DateFormatUtils.convertDateTimeToString(endDate)+";百分比="+rate+";上升还是下降="+updown+"当前页"+assistDto.getCurrentPages()+"总共assistDto.getCurrentPages()"+assistDto.getCurrentPages());
		/**
		 * 设置当前页
		 */
		if (0 == assistDto.getCurrentPages()) {
			assistDto.setCurrentPages(1);
		}
		/**
		 * 偏移值= (当前第几页-1)*每页个数
		 */
		int offset = (assistDto.getCurrentPages() - 1) * limit;
		/**
		 * 查询得到的总记录数
		 */
		int num=stoFinancePerStockService.countSearchWithNetIncome(startDate, endDate, rate,updown);
		System.err.println("--查询得到的总记录数-----"+num+"-----");
		/**
		 * 总共多少页
		 */
		int totalPages = (num / limit);
		if (num % limit > 0) {
			totalPages++;
		}
		if (assistDto.getCurrentPages() >totalPages) {
			assistDto.setCurrentPages(1); 
			return false;
		}
		System.out.println(totalPages+"总页数");
		assistDto.setTotalPages(totalPages);
		
		HashMap<String,List> map=stoFinancePerStockService.querySearchWithNetIncome(startDate, endDate, rate,updown,offset,limit); 
		this.stoFinanceperstockDTOList=map.get("stoFinanceperstockDTOList");
		this.stockDTOList=map.get("stockDTOList");
		return true;
	}
	
	/**
	 * 处理”每股净资产“，默认情况处理
	 * @return
	 * @throws Exception
	 */
	public String prepareNetAsset() throws Exception{
		assistDto.setStartDate(DateFormatUtils.convertStringToDate("2008-12-31"));
		assistDto.setEndDate(DateFormatUtils.convertStringToDate("2009-12-31"));
		assistDto.setRate(20);
		assistDto.setUpdown(1);
		
		this.doNetAssetSearch();
		
		return "search";
	}
	
	/**
	 * 处理”每股净资产“，用户搜索
	 * @return
	 * @throws Exception
	 */
	public String searchNetAsset() throws Exception{
		if (!this.doNetAssetSearch()) {
			return "prepare";
		}
		return "search";
	}
	
	/**
	 * 处理”每股净资产“，详细信息显示
	 * @return
	 * @throws Exception
	 */
	public String netAssetDetail() throws Exception{
		this.stoFinanceperstockDTOList=stoFinancePerStockService.queryStoFinancePerStockByStockCode(stockDTO.getStockCode());
		stockDTO=stoStockService.findByStockCode(stockDTO.getStockCode());
		
		return "detail";
	}
	
	private boolean doNetAssetSearch(){
		Date startDate=assistDto.getStartDate();
		Date endDate=assistDto.getEndDate();
		double rate=assistDto.getRate();
		int updown=assistDto.getUpdown();
		System.out.println("开始时间="+DateFormatUtils.convertDateTimeToString(startDate)+";结束时间="
				+DateFormatUtils.convertDateTimeToString(endDate)+";百分比="+rate+";上升还是下降="+updown+"当前页"+assistDto.getCurrentPages()+"总共assistDto.getCurrentPages()"+assistDto.getCurrentPages());
		/**
		 * 设置当前页
		 */
		if (0 == assistDto.getCurrentPages()) {
			assistDto.setCurrentPages(1);
		}
		/**
		 * 偏移值= (当前第几页-1)*每页个数
		 */
		int offset = (assistDto.getCurrentPages() - 1) * limit;
		/**
		 * 查询得到的总记录数
		 */
		int num=stoFinancePerStockService.countSearchWithNetAsset(startDate, endDate, rate,updown);
		System.err.println("--查询得到的总记录数-----"+num+"-----");
		/**
		 * 总共多少页
		 */
		int totalPages = (num / limit);
		if (num % limit > 0) {
			totalPages++;
		}
		if (assistDto.getCurrentPages() >  totalPages) {
			assistDto.setCurrentPages(1);
			return false;
		}
		System.out.println(totalPages+"总页数");
		assistDto.setTotalPages(totalPages);
		
		HashMap<String,List> map=stoFinancePerStockService.querySearchWithNetAsset(startDate, endDate, rate,updown,offset,limit); 
		this.stoFinanceperstockDTOList=map.get("stoFinanceperstockDTOList");
		this.stockDTOList=map.get("stockDTOList");
		this.oldYearStoFinanceperstockDTOList=map.get("oldYearStoFinanceperstockDTOList");
		
		return true;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	public FinancePerStockAction(){
		if(null==assistDto){
			this.assistDto=new AssistDto(); 
		}
		if(null==stockDTO){
			this.stockDTO=new StoStockDTO(); 
		}
		if(null==stockDTOList){
			this.stockDTOList=new ArrayList<StoStockDTO>(); 
		}
		if(null==stoFinanceperstockDTOList){
			this.stoFinanceperstockDTOList=new ArrayList<StoFinanceperstockDTO>(); 
		}
	}
	
	@Override
	public StoFinanceperstockDTO getModel() {
		if(null == this.stoFinanceperstockDTO){
			this.stoFinanceperstockDTO=new StoFinanceperstockDTO();
		}
		return this.stoFinanceperstockDTO;
	}

	public StoFinanceperstockDTO getStoFinanceperstockDTO() {
		return stoFinanceperstockDTO;
	}

	public void setStoFinanceperstockDTO(StoFinanceperstockDTO stoFinanceperstockDTO) {
		this.stoFinanceperstockDTO = stoFinanceperstockDTO;
	}

	public List<StoStockDTO> getStockDTOList() {
		return stockDTOList;
	}

	public void setStockDTOList(List<StoStockDTO> stockDTOList) {
		this.stockDTOList = stockDTOList;
	}

	public AssistDto getAssistDto() {
		return assistDto;
	}

	public void setAssistDto(AssistDto assistDto) {
		this.assistDto = assistDto;
	}

	public StoStockDTO getStockDTO() {
		return stockDTO;
	}

	public void setStockDTO(StoStockDTO stockDTO) {
		this.stockDTO = stockDTO;
	}

	public StoStockService getStoStockService() {
		return stoStockService;
	}

	public void setStoStockService(StoStockService stoStockService) {
		this.stoStockService = stoStockService;
	}

	public StoFinancePerStockService getStoFinancePerStockService() {
		return stoFinancePerStockService;
	}

	public void setStoFinancePerStockService(
			StoFinancePerStockService stoFinancePerStockService) {
		this.stoFinancePerStockService = stoFinancePerStockService;
	}

	public List<StoFinanceperstockDTO> getStoFinanceperstockDTOList() {
		return stoFinanceperstockDTOList;
	}

	public void setStoFinanceperstockDTOList(
			List<StoFinanceperstockDTO> stoFinanceperstockDTOList) {
		this.stoFinanceperstockDTOList = stoFinanceperstockDTOList;
	}

	public List<StoFinanceperstockDTO> getOldYearStoFinanceperstockDTOList() {
		return oldYearStoFinanceperstockDTOList;
	}

	public void setOldYearStoFinanceperstockDTOList(
			List<StoFinanceperstockDTO> oldYearStoFinanceperstockDTOList) {
		this.oldYearStoFinanceperstockDTOList = oldYearStoFinanceperstockDTOList;
	}
	
	
}
